package edu.guet.cn.blogbasics.service.impl;

import edu.guet.cn.blogbasics.entity.JwtAuthUser;
import edu.guet.cn.blogbasics.entity.Users;
import edu.guet.cn.blogbasics.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Auther: hejianding
 * @Date: 2021/4/8 10:37
 * @Version: 1.0
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UsersMapper usersMapper;

    /**
     * 通过用户名从数据库读取该用户的账户信息及权限信息
     * @param
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        Users users = usersMapper.findByName(userName);
        if (users==null){
            //表明用户不存在
            throw new UsernameNotFoundException("用户不存在");
        }
        users.setRoles(usersMapper.getRolesByUserName(userName));
        System.out.println("UserDetailsServiceImpl==>loadUserByUsername:" +users.toString());
        return new JwtAuthUser(users);
    }
}
